<!DOCTYPE html>
<html>
<head>
<title>dpmDetail.html</title>

<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="科室信息窗口">
<meta name="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="/Hospital/lib/layui-v2.1.6/layui/css/layui.css" media="all">
<style type="text/css">
select {
	height: 38px;
	line-height: 1.3;
	line-height: 38px\9;
	border-width: 1px;
	border-style: solid;
	background-color: #fff;
	border-radius: 2px;
	width: 100%;
}

.iCss {
	color: red;
	padding-right: 5px;
}

.row {
	margin-top: 10px;
}
</style>
</head>
<body>
	<div class="layui-container" style="">
		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1">
				<span class="layui-form-label">门诊名称: </span>
			</div>
			<div class="layui-col-xs5">
				<select id="clinic" name="clinic" class="layui-input-inline">
				</select>
			</div>
		</div>
		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1">
				<span class="layui-form-label"><i class="iCss">*</i>科室名称: </span>
			</div>
			<div class="layui-col-xs5">
				<input id="dpmName" class="layui-input" type="text">
			</div>
		</div>
		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1">
				<span class="layui-form-label">科室负责人: </span>
			</div>
			<div class="layui-col-xs5 ">
				<select id="principal" name="principal" class="layui-input-inline">
				</select>
			</div>
		</div>
		<div class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1">
				<span class="layui-form-label">联系电话: </span>
			</div>
			<div class="layui-col-xs5">
				<input id="phone" class="layui-input" type="text">
			</div>
		</div>
		<div id="div_createTime" class="layui-row row">
			<div class="layui-col-xs4 layui-col-xs-offset1">
				<span class="layui-form-label">创建时间: </span>
			</div>
			<div class="layui-col-xs5">
				<input id="createTime" class="layui-input" type="text" disabled>
			</div>
		</div>
	</div>
</body>
<script src="/Hospital/lib/jquery.min.js" charset="utf-8"></script>
<script src="/Hospital/lib/layui-v2.1.6/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
	layui.use([ "layer" ], function() {
		var $ = layui.jquery;
		var layer = layui.layer;

		initFrame();//初始化表单数据
		initEvent();//初始化事件

		//初始化表单数据
		function initFrame() {
			var operator = localStorage.getItem("dpmTable_operator");
			if (operator == 1) {
				//如果是新建科室
				initAddFrame();
			}
			if (operator == 2) {
				//如果是修改科室
				initAlterFrame();
			}
		}

		//初始修改科室界面
		function initAlterFrame() {
			var dpmData = JSON.parse(localStorage.getItem("dpmDetail"));
			var permission = JSON.parse(localStorage.getItem("loginUser")).permission;
			//$("#clinicName").val(dpmData.clinicName);
			console.log("initAlterFrame.dpmData");
			console.log(dpmData);
			$("#clinic").append(
					"<option value="+dpmData.clinicId+">" + dpmData.clinicName + "</option>");

			$("#dpmName").val(dpmData.departmentName);
			$("#address").val(dpmData.address);
			$("#phone").val(dpmData.phone);
			$("#createTime").val(dpmData.createTime);

			if (permission.alterDepartment == 3) {
				//管理员
				var param = {
					clinicId : dpmData.clinicId,
				};
				initAlterPrincipleSelect(param, dpmData);
			} else if (permission.alterDepartment >= 1) {
				//门诊负责人
				$("#principal").attr("disabled", true);
				$("#principal").append(
						"<option value="+dpmData.principalId+" _code="+dpmData.principalCode+">"
								+ dpmData.principalName + "</option>");
			}
		}

		//初始添加科室界面
		function initAddFrame() {
			var loginUser = JSON.parse(localStorage.getItem("loginUser"));
			$("#clinicName").val(loginUser.clinicName);
			$("#div_createTime").css({
				"display" : "none"
			});

			var param = {
				clinicId : loginUser.clinicId,
			};
			initAddPrincipleSelect(param); //初始化负责人选框

			$.ajax({
				type : "post",
				url : "/Hospital/clinicController/getAllClinic",
				data : param,
				success : function(response) {
					if (response.code != 200) {
						console.error(response);
					}
					var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var clinicId = result[i].clinicId;
						var clinicName = result[i].clinicName;
						$("#clinic").append(
								"<option value="+clinicId+">" + clinicName + "</option>");
					}
				},
			});
		}
		
		//初始化编辑窗口的科室候选人下拉框
		function initAlterPrincipleSelect(param, dpmData) {
			$.ajax({
				type : "post",
				url : "/Hospital/userController/getDepartmentPrincipal",
				data : param,
				success : function(response) {
					if (response.code != 200 && response.code != 414) {
						console.error(response);
						return;
					}
					if (response.code == 414) {
						layer.msg("该门诊不存在科室负责人候选人");
						return;
					}
					var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var employeeId = result[i].employeeId;
						var employeeCode = result[i].employeeCode;
						var name = result[i].name;
						$("#principal").append(
								"<option value="+employeeId+" _code="+employeeCode+">" + name
										+ "</option>");
					}
					$("#principal").val(dpmData.principalId);
				},
			});
		}
		
		//初始化增加窗口的科室候选人下拉框
		function initAddPrincipleSelect(param) {
			$.ajax({
				type : "post",
				url : "/Hospital/userController/getDepartmentPrincipal",
				data : param,
				success : function(response) {
					if (response.code != 200 && response.code != 414) {
						console.error(response);
						return;
					}
					if (response.code == 414) {
						layer.msg("该门诊不存在科室负责人候选人");
						return;
					}
					var result = response.result;
					for (var i = 0; i < result.length; i++) {
						var employeeId = result[i].employeeId;
						var employeeCode = result[i].employeeCode;
						var name = result[i].name;
						$("#principal").append(
								"<option value="+employeeId+" _code="+employeeCode+">" + name
										+ "</option>");
					}
				},
			});
		}

		//初始化事件
		function initEvent() {
			$("#clinic").change(function() {
				var param = {
					clinicId : $(this).val(),
				};
				$("#principal").empty();
				initAddPrincipleSelect(param);
			});
		}
	});

	function getParam() {
		var param = {};
		var operator = localStorage.getItem("dpmTable_operator");
		if (operator == 1) {
			//如果是新建科室
			var loginUser = JSON.parse(localStorage.getItem("loginUser"));
			param = {
				clinicId : $("#clinic").val(),
				name : $("#dpmName").val(),
				phone : $("#phone").val(),
				employeeId : $("#principal").val(),
			};
		}
		if (operator == 2) {
			//如果是修改科室
			var dpmData = JSON.parse(localStorage.getItem("dpmDetail"));
			param = {
				departmentId : dpmData.departmentId,
				clinicId : dpmData.clinicId,
				name : $("#dpmName").val(),
				phone : $("#phone").val(),
				employeeId : $("#principal").val(),
			};
			//console.log("dpmDetail/getParam.param");
			//console.log(param);
		}

		return param;
	}
</script>
</html>
